//
//  BSMeViewController.m
//  BaiSi
//
//  Created by Sycamore on 16/4/27.
//  Copyright © 2016年 Sycamore. All rights reserved.
//

#import "BSMeViewController.h"
#import "BSSettingViewController.h"
#import <AFNetworking/AFNetworking.h>
#import "BSSquareItem.h"
#import <MJExtension/MJExtension.h>
#import "BSSquareCell.h"
#import <SafariServices/SafariServices.h>
#import "BSWebViewController.h"
#import "BSNavigationController.h"


static NSInteger const  itemSpacing = 1;
static NSInteger const  colums = 4;
#define ItemWH          ((BSScreenWidth - (colums - 1) * itemSpacing)/ colums)

@interface BSMeViewController () <UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic,strong) NSMutableArray *squareList;
@property (nonatomic,weak) UICollectionView *collectionView;
@property (nonatomic,assign) CGFloat itemHeight;
@end

@implementation BSMeViewController

static NSString * const ID = @"cell";

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"我的";
    
    UIBarButtonItem *item1 = [UIBarButtonItem itemWithImage:[UIImage imageNamed:@"mine-setting-icon"] highlightedImage:[UIImage imageNamed:@"mine-setting-icon-click"] target:self action:@selector(setting)];
    
    UIBarButtonItem *item2 = [UIBarButtonItem itemWithImage:[UIImage imageNamed:@"mine-moon-icon"] selectedImage:[UIImage imageNamed:@"mine-moon-icon-click"] target:self action:@selector(night:)];
    
    self.navigationItem.rightBarButtonItems = @[item1,item2];
    self.tableView.backgroundColor = BSColor(206, 206, 206);
    
    self.tableView.sectionHeaderHeight = 0;
    self.tableView.sectionFooterHeight = 10;
    self.tableView.contentInset = UIEdgeInsetsMake(-25, 0, 0, 0);
    
    
    //获取网络数据
    [self loadData];
    
    
    //创建tableFooterView
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    CGFloat itemWidth = ItemWH;
    CGFloat itemHeight = itemWidth;
    layout.minimumInteritemSpacing = itemSpacing;
    layout.minimumLineSpacing = itemSpacing;
    layout.itemSize = CGSizeMake(itemWidth, itemHeight);
    _itemHeight = itemHeight + itemSpacing;
    
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
    _collectionView = collectionView;
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    collectionView.backgroundColor = [UIColor lightGrayColor];
    self.tableView.tableFooterView = collectionView;
    
    [collectionView registerNib:[UINib nibWithNibName:@"BSSquareCell" bundle:nil] forCellWithReuseIdentifier:ID];
    
}

- (void)loadData
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSDictionary *param = @{
                            @"a" : @"square",
                            @"c" : @"topic"
                            };
    [manager GET:BSBaseURL parameters:param progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
//        NSLog(@"%@",responseObject);
        
        _squareList = [BSSquareItem mj_objectArrayWithKeyValuesArray:responseObject[@"square_list"]];

        [self resoveData];
        
        [_collectionView reloadData];
        
                //刷新tableView的内容
        self.tableView.tableFooterView = _collectionView;
    } failure:nil];
}

- (void)resoveData
{
    //计算行数
    NSInteger rowCount = (_squareList.count - 1) / colums + 1;
    _collectionView.bs_height = rowCount * _itemHeight;
    
    NSInteger extra = _squareList.count % colums;
    
    if (extra >0) {
        NSInteger left = colums - extra;
        for (int i  = 0; i < left; i ++) {
             BSSquareItem *item = [[BSSquareItem alloc]init];
            [_squareList addObject:item];
        }
    }
}

- (void)night:(UIButton *)button
{
    button.selected = !button.selected;
}

- (void)setting
{
    BSSettingViewController *setting = [[BSSettingViewController alloc]init];
    [self.navigationController pushViewController:setting animated:YES];
}

#pragma mark - 
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _squareList.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    BSSquareCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    cell.item = _squareList[indexPath.row];
    
    return cell;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    BSSquareItem *item = _squareList[indexPath.row];
    if ([item.url containsString:@"http"]) {
//        SFSafariViewController *sf = [[SFSafariViewController alloc]initWithURL:[NSURL URLWithString:item.url]];
//        
//        [self presentViewController:sf animated:YES completion:nil];
        BSWebViewController *webVc = [[BSWebViewController alloc]init];
        webVc.url = item.url;
        [self.navigationController pushViewController:webVc animated:YES];

    }
    
}
@end
